/**
 * 
 */
package neptune.web.client.view.components.editor;

import neptune.web.client.model.topology.SerializableLink;
import neptune.web.client.model.topology.SerializableTopology;

import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class RemoveLinkCommand implements Command {

	private SerializableLink link;
	private SerializableTopology topology;
	private TreeItem treeItem;
	private TopologyEditor editor;

	public RemoveLinkCommand(SerializableLink link,
			SerializableTopology topology, TreeItem treeItem,
			TopologyEditor editor) {
		this.link = link;
		this.topology = topology;
		this.treeItem = treeItem;
		this.editor = editor;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.Command#execute()
	 */
	public void execute() {
		topology.removeLink(link);
		treeItem.remove();

		// Codice per la rimozione del disegno del link
		Widget w1 = null, w2 = null;
		for (int i = 0; i < editor.getGraphPanel().getWidgetCount(); i++) {
			if (editor.getGraphPanel().getWidget(i) instanceof Label) {
				Label label = (Label) editor.getGraphPanel().getWidget(i);
				if (label.getText().equals(link.getSource().getNodeId()))
					w1 = label;
				else if (label.getText().equals(
						link.getDestination().getNodeId()))
					w2 = label;
			}
		}
		if (w1 != null && w2 != null) {
			editor.getGraphPanel().removeConnection(w1, w2);
			editor.updateGraphics();
		}
	}

}
